﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using MockupDesigner.Editors.TextEditors.Lists;
using MockupDesigner.AppItems;

namespace MockupDesigner.Items
{
    [MenuEditor]
    public class MenuItem : ResizableItem
    {
        public static readonly DependencyProperty ItemsProperty =
          DependencyProperty.Register("Items", typeof(string[]), typeof(MenuItem));

        private string[] items;
        public string[] Items
        {
            get
            {
                return (string[])GetValue(ItemsProperty) == null ? this.items : (string[])GetValue(ItemsProperty);
            }
            set
            {
                string[] oldVal = Items;
                if (AreItemsEqual(Items, value))
                {
                    return;
                }
                this.autochanging = true;
                OnPropertyChanging("Items", oldVal, value);
                SetControls(value);
                this.items = value;
                if (this.Parent is DesignerItem)
                {
                    base.Height = items.Length * 25;
                    int max = 25;
                    foreach (string s in items)
                    {
                        int length = items.Length * 15;
                        if (length > max)
                        {
                            max = length;
                        }
                    }
                    base.Width = max + 70;
                }
                OnPropertyChanged("Items");
                this.autochanging = false;
            }
        }

        private void SetControls(string[] items)
        {
            System.Windows.Controls.Grid g = (this.Content as Border).Child as System.Windows.Controls.Grid;
            StackPanel leftPanel = g.Children[0] as StackPanel;
            StackPanel rightPanel = g.Children[1] as StackPanel;
            leftPanel.Children.Clear();
            rightPanel.Children.Clear();
            foreach (string item in items)
            {
                string[] line = item.Split(new string[]{"|"},StringSplitOptions.RemoveEmptyEntries);
                string label = string.Empty;
                string shortcut = string.Empty;
                if (line.Length == 2)
                {
                    label = line[0];
                    shortcut = line[1];
                }
                else
                {
                    label = item;
                }
                if (label.Trim() != "-")
                {
                    Label leftLabel = new Label() { Content = label, HorizontalAlignment = HorizontalAlignment.Left };
                    leftLabel.SetResourceReference(Label.StyleProperty, "SimpleLabelStyle");
                    leftPanel.Children.Add(leftLabel);
                    Label rightLabel = new Label() { Content = shortcut, HorizontalAlignment = HorizontalAlignment.Left };
                    rightLabel.SetResourceReference(Label.StyleProperty, "SimpleLabelStyle");
                    rightPanel.Children.Add(rightLabel);
                }
                else
                {
                    Separator leftSeparator = new Separator();
                    leftSeparator.SetResourceReference(Label.StyleProperty, "SeparatorStyle");
                    leftPanel.Children.Add(leftSeparator);
                    Separator rightSeparator = new Separator();
                    rightSeparator.SetResourceReference(Label.StyleProperty, "SeparatorStyle");
                    rightPanel.Children.Add(rightSeparator);
                }
            }
        }

        private bool AreItemsEqual(string[] items, string[] value)
        {
            if (items == null && value == null)
                return true;
            if (items == null && value != null)
                return false;
            if (items != null && value == null)
                return false;
            if (items.Length != value.Length)
            {
                return false;
            }
            for(int i=0;i<items.Length;i++)
            {
                if (items[i] != value[i])
                {
                    return false;
                }
            }
            return true;
        }
        

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            string[] init = new string[4];
            init[0] = "New|CTRL+N";
            init[1] = "Open|CTRL+O";
            init[2] = "-";
            init[3] = "Quit|CTRL+Q";
            this.Items = init;

            
        }
    }
}
